class Content < ActiveRecord::Base
	belongs_to :folder
	has_and_belongs_to_many :user_groups

  can_be :folder, :as => "presentable"
  can_be :page, :as => "presentable"
  can_be :message, :as => "presentable"
  can_be :function, :as => "presentable"
  can_be :document_category, :as => "presentable"
  can_be :document, :as => "presentable"
  can_be :document_version, :as => "presentable"

  can_be :gallery, :as => "presentable"
  can_be :picture, :as => "presentable"

	validates_presence_of :name
	validates_uniqueness_of :url, :if => :url

	#Don't allow dashes in name, it will complicate the nice urls
	validates_format_of(:name, :with => /^[&\w\ ]*$/)

  after_save :after_save_tasks

  class << self
    def find_as_folder(folder_id)
      find :first, :conditions => as_folder_condition(folder_id) 
    end

    def top_level_folders
      #Just make sure Rails finds and loads the Folder AR class
      Folder
      cond = <<-SQL
        contents.presentable_type = 'Folder'
        and contents.folder_id is null
      SQL

      find :all, 
        :conditions => cond, 
        :order => "contents.position"
    end # top_level_folders

    def contents_for_folder(folder_id)
      return [] unless folder_id
      find(:all, :conditions => ["contents.folder_id = ?", folder_id])
    end

    def as_folder_condition(id)
      return <<-SQL
        contents.presentable_type = 'Folder'
        and contents.presentable_id = #{id}
      SQL
    end
  end # class block

  #
  # This method will be called when the content type is a folder
  #
  def contents_for_folder
    return unless self.is_a_folder?
    Content.contents_for_folder(self.presentable_id)
  end # contents_for_folder

  def all_children
    return [] unless self.is_a_folder?
    all_contents = []
    children = Content.find(:all, :conditions => ["contents.folder_id = ?", self.presentable_id])
    children.each do |child|
      if child.is_a_folder?
        all_contents << child
        all_contents += child.all_children
      else
        all_contents << child
      end
    end
    all_contents
  end


  def parent_names
    if self.folder_id
      dad = Content.find_as_folder(self.folder_id) 
      dad.parent_names + "::" + self.name 
    else
      self.name
    end
  end # parent_names

  def set_qualified_name
    qname = parent_names
    connection.execute("update contents set qualified_name = '#{qname}' where contents.id = #{self.id}")
  end # set_qualified_name
	
  protected
  
  def validate
    if Easel::Config.reserved_urls.include?(self.url)
      self.errors.add(:url, "is a reserved url.  Please select another one.")
    end
  end # validate

	private
  def after_save_tasks
    set_qualified_name
    sync_user_groups_with_parent
    maybe_set_url
  end

  def sync_user_groups_with_parent
    return unless self.folder_id
    
    parent = Content.find_as_folder(self.folder_id)
    parent.user_groups.each do |pug|
      next if user_group_ids.include?(pug.id)
      connection.execute <<-SQL
       insert into contents_user_groups(content_id, user_group_id) 
          values(#{self.id}, #{pug.id})
      SQL
    end
  end # sync_user_groups_with_parent

	def maybe_set_url
    if self.url.blank?
      permalink = UrlVersion.unique_url_for(self.name)
      connection.execute("update contents set url = '#{permalink}' where contents.id = #{self.id}")
    end
  end # set_url

end # Content 
